package cn.itcast.service.cargo.impl;


import cn.itcast.dao.cargo.ExportDao;
import cn.itcast.dao.cargo.PackingListDao;
import cn.itcast.domain.ResultInfo;
import cn.itcast.domain.cargo.Export;
import cn.itcast.domain.cargo.PackingList;
import cn.itcast.domain.cargo.ShippingOrder;
import cn.itcast.service.cargo.PackingListService;
import com.alibaba.dubbo.config.annotation.Service;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author zhou
 * @since 2021-01-28
 */
@Service
public class PackingListServiceImpl extends ServiceImpl<PackingListDao, PackingList> implements PackingListService {

    @Autowired
    private ExportDao exportDao;

    @Override
    public PageInfo<PackingList> findByPage(Integer pageNum, Integer pageSize, String loginUserCompanyId) {
        PageHelper.startPage(pageNum,pageSize);

        List<PackingList> packingLists = this.list(new QueryWrapper<PackingList>().eq("company_id", loginUserCompanyId));

        PageInfo<PackingList> pageInfo = new PageInfo<>(packingLists);
        return pageInfo;
    }

    @Override
    public ResultInfo delete(String packingListId) {
        PackingList packingList = baseMapper.selectById(packingListId);

        int count = baseMapper.delete(new QueryWrapper<PackingList>().eq("packing_list_id", packingListId).ne("state", 1));
        if (count==1){
            //packingList删除成功，修改出货单状态
            String exportIds = packingList.getExportIds();
            String[] split = exportIds.split(",");
            if (split.length != 0){
                exportDao.updateStateByIds(split,2);
            }

            return new ResultInfo(true);
        }else {
            return new ResultInfo(false,"删除失败，请检查装箱单状态！");
        }
    }

    @Override
    public ResultInfo shipping(String packingListId) {
        //根据packingListId查询装箱单
        PackingList packingList = baseMapper.selectById(packingListId);
        //查看装箱单是否已委托，否则报错
        if (packingList.getState()!=0){
            return new ResultInfo(false,"该装箱单已委托");
        }
        //创建委托订单
        ShippingOrder shippingOrder = new ShippingOrder();
        shippingOrder.setShippingOrderId(packingListId);

        //lcNo 信用证
        String exportId = packingList.getExportIds().split(",")[0];
        Export export = exportDao.selectByPrimaryKey(exportId);
        shippingOrder.setLcNo(export.getLcno());

        //portOfLoading  装运港
        shippingOrder.setPortOfLoading(packingList.getShipmentPort());

        //portOfDischarge  卸货港
        shippingOrder.setPortOfDischarge(packingList.getDestinationPort());

        List<Object> data = new ArrayList<>();
        data.add(shippingOrder);
        data.add(packingList.getMarks());
        //返回数据
        return new ResultInfo(true,"",data);
    }

    @Override
    public void savePacking(PackingList packingList) {
        //修改出货单状态
        String exportIds = packingList.getExportIds();
        String[] split = exportIds.split(",");
        if (split.length != 0){
            exportDao.updateStateByIds(split,3);
        }

        baseMapper.insert(packingList);
    }

    @Override
    public PackingList findById(String id) {
        return baseMapper.selectById(id);
    }
}
